package	com.sdry.web.controller.plan;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.ui.Model;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;

import java.io.IOException;

import com.sdry.model.plan.DispatchBillDetail;
import com.sdry.model.plan.StorageArea;
import com.sdry.service.plan.DispatchBillDetailService;
import com.sdry.service.plan.StorageAreaService;
import com.sdry.utils.ResponseUtil;
import com.sdry.web.controller.tdd.emailRecord.EmailRecordController;
/**
 *
 *@ClassName: DispatchBillDetailController
 *@Description: 发货计划详情
 *@Author llm
 *@Date 2019-07-11 09:10:30
 *@version 1.0
*/
@Controller
@RequestMapping("/DispatchBillDetail")
public class DispatchBillDetailController{
	@Resource DispatchBillDetailService DispatchBillDetailService;
	@Resource StorageAreaService storageAreaService;
	//声明一个Logger
	private final static Logger logger = LoggerFactory.getLogger(DispatchBillDetailController.class);
	/** 
	 * 进入页面
	 * @param model
	 * @return 页面路径
	*/
	@RequestMapping("/page")
	public String pageDispatchBillDetail (Model model) {
		return "/plan/DispatchBillDetail";
	}
	/** 
	 * 根据主键id查询实体
	 * @param id 主键id
	 * @return 实体
	*/
	@RequestMapping("/queryById")
	@ResponseBody
	public DispatchBillDetail queryById(Long id) {
		DispatchBillDetail param = DispatchBillDetailService.queryById(id);
		return param;
	}
	/** 
	 * 条件查询所有
	 * @param param 实体条件
	 * @return 实体集合
	*/
	@RequestMapping("/queryAllByMution")
	@ResponseBody
	public List<DispatchBillDetail> queryAllByMution(DispatchBillDetail param) {
		List<DispatchBillDetail> list = DispatchBillDetailService.queryAllByMution(param);
		return list;
	}
	/** 
	 * 条件分页查询
	 * @param param 实体条件
	 * @param response
	 * @throws Exception
	*/
	@RequestMapping("/list")
	public void list(DispatchBillDetail param,HttpServletResponse response){
		/*
		 * 条件分页查询内容
		 */
		List<DispatchBillDetail> list = DispatchBillDetailService.findPageByMution(param);
		/*
		 * 条件查询总条数
		 */
		Integer total = DispatchBillDetailService.selectCountByMution(param);
		/*
		 * 转为json数据传至页面
		 */
		JSONObject jsonObject = new JSONObject();
		JsonConfig jsonConfig = new JsonConfig();
		JSONArray jsonArray = JSONArray.fromObject(list, jsonConfig);
		jsonObject.put("code", 0);
		jsonObject.put("data", jsonArray);
		jsonObject.put("count", total);
		try {
			ResponseUtil.write(response, jsonObject);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	/** 
	 * 插入返回id
	 * @param param 实体条件
	 * @param response
	 * @throws Exception
	*/
	@RequestMapping("/insert")
	public void insert(DispatchBillDetail param, HttpServletResponse response){
		DispatchBillDetail billDetail = new DispatchBillDetail();
		billDetail.setMaterielId(param.getMaterielId());
		billDetail.setDispatchId(param.getDispatchId());
		List<DispatchBillDetail> billDetails = DispatchBillDetailService.queryAllByMution(billDetail);
		Long id = 0L;
		if(billDetails.size() <= 0){
			//根据物料获取线边库库存和安全库存
			StorageArea storageArea = new StorageArea();
			storageArea.setMaterielId(param.getMaterielId());
        	List<StorageArea> li = storageAreaService.queryAllByMution(storageArea);
        	if(li.size() > 0){
        		//线边库存
        		param.setStorageQuantity(li.get(0).getMaterielQuantity());
        		if(null != li.get(0).getLowers()) {
        			//线边安全库存
        			param.setAstock(li.get(0).getLowers());
        		}else {
        			//线边安全库存
        			param.setAstock(0);
        		}
        	}else{
        		//线边库存
        		param.setStorageQuantity(0);
        		//线边安全库存
        		param.setAstock(0);
        	}
			id = DispatchBillDetailService.insert(param);
		}
		try {
			response.getWriter().print(id);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	/** 
	 * 根据主键更新 返回影响行数
	 * @param param 实体条件
	 * @param response
	 * @throws Exception
	*/
	@RequestMapping("/update")
	public void update(DispatchBillDetail param,HttpServletResponse response){
		Integer count = DispatchBillDetailService.update(param);
		try {
			response.getWriter().print(count);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	/** 
	 * 根据主键拼接的字符串删除返回影响行数
	 * @param ids 主键拼接的字符串
	 * @return 影响行数
	*/
	@RequestMapping("/delete")
	public void delete(String ids,HttpServletResponse response){
		ids=ids.replace("'",""); 
		Integer count = DispatchBillDetailService.delete(ids);
		try {
			response.getWriter().print(count);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	/** 
	 * 根据发货计划ID拼接的字符串删除返回影响行数
	 * @param ids 发货计划ID拼接的字符串
	 * @return 影响行数
	 */
	@RequestMapping("/deleteByDispatchIds")
	public void deleteByDispatchIds(String ids,HttpServletResponse response){
		logger.info("根据发货计划ID拼接的字符串删除返回影响行数"+ids);
		ids=ids.replace("'",""); 
		Integer count = DispatchBillDetailService.deleteByDispatchIds(ids);
		logger.info("根据发货计划ID拼接的字符串删除返回影响行数"+count);
		try {
			response.getWriter().print(count);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
